// Tauri-specific type definitions

import { UnlistenFn } from '@tauri-apps/api/event';

// Tauri API types
export interface TauriInvokeOptions {
  timeout?: number;
}

export interface TauriEventOptions {
  once?: boolean;
}

// Window management types
export interface WindowOptions {
  label: string;
  url?: string;
  width?: number;
  height?: number;
  minWidth?: number;
  minHeight?: number;
  maxWidth?: number;
  maxHeight?: number;
  x?: number;
  y?: number;
  center?: boolean;
  resizable?: boolean;
  maximized?: boolean;
  visible?: boolean;
  decorations?: boolean;
  alwaysOnTop?: boolean;
  fullscreen?: boolean;
  transparent?: boolean;
  title?: string;
  icon?: string;
}

export interface WindowState {
  isVisible: boolean;
  isMaximized: boolean;
  isMinimized: boolean;
  isFullscreen: boolean;
  isFocused: boolean;
  isDecorated: boolean;
  isResizable: boolean;
  isAlwaysOnTop: boolean;
  position: { x: number; y: number };
  size: { width: number; height: number };
}

// File system types
export interface FileEntry {
  path: string;
  name?: string;
  children?: FileEntry[];
}

export interface FileDialogOptions {
  title?: string;
  defaultPath?: string;
  filters?: FileDialogFilter[];
  multiple?: boolean;
  directory?: boolean;
}

export interface FileDialogFilter {
  name: string;
  extensions: string[];
}

export interface SaveDialogOptions {
  title?: string;
  defaultPath?: string;
  filters?: FileDialogFilter[];
}

// Notification types
export interface NotificationOptions {
  title: string;
  body?: string;
  icon?: string;
  sound?: string;
  timeout?: number;
}

export interface NotificationPermission {
  granted: boolean;
  denied: boolean;
  default: boolean;
}

// System tray types
export interface SystemTrayOptions {
  id?: string;
  icon?: string;
  tooltip?: string;
  title?: string;
  menu?: SystemTrayMenu;
}

export interface SystemTrayMenu {
  items: SystemTrayMenuItem[];
}

export interface SystemTrayMenuItem {
  id: string;
  text: string;
  enabled?: boolean;
  checked?: boolean;
  accelerator?: string;
  submenu?: SystemTrayMenuItem[];
}

export interface SystemTrayEvent {
  id: string;
  type: 'click' | 'double_click' | 'right_click';
  position: { x: number; y: number };
}

// Global shortcut types
export interface GlobalShortcut {
  shortcut: string;
  callback: () => void;
}

export interface ShortcutEvent {
  shortcut: string;
  id: string;
}

// App types
export interface AppInfo {
  name: string;
  version: string;
  tauriVersion: string;
  identifier: string;
}

export interface AppConfig {
  theme: 'light' | 'dark' | 'system';
  language: string;
  autoStart: boolean;
  minimizeToTray: boolean;
  closeToTray: boolean;
  startMinimized: boolean;
  notifications: boolean;
  updateCheck: boolean;
  telemetry: boolean;
  shortcuts: {
    toggleWindow: string;
    quit: string;
    minimize: string;
    maximize: string;
    fullscreen: string;
  };
  window: {
    width: number;
    height: number;
    minWidth: number;
    minHeight: number;
    x?: number;
    y?: number;
    maximized: boolean;
    alwaysOnTop: boolean;
    decorations: boolean;
    transparent: boolean;
  };
}

// Process types
export interface ProcessInfo {
  pid: number;
  name: string;
  cpu: number;
  memory: number;
}

export interface SystemInfo {
  platform: string;
  arch: string;
  version: string;
  hostname: string;
  uptime: number;
  totalMemory: number;
  freeMemory: number;
  cpuCount: number;
  loadAverage: number[];
}

// Update types
export interface UpdateInfo {
  version: string;
  date: string;
  body: string;
  signature?: string;
}

export interface UpdateStatus {
  available: boolean;
  currentVersion: string;
  latestVersion?: string;
  info?: UpdateInfo;
}

export interface UpdateProgress {
  downloaded: number;
  total: number;
  percentage: number;
  speed: number;
  eta: number;
}

// Event types
export interface TauriEvent<T = any> {
  event: string;
  payload: T;
  windowLabel: string;
  id: number;
}

export interface EventCallback<T = any> {
  (event: TauriEvent<T>): void;
}

export interface EventUnsubscribe {
  (): void;
}

// Command types
export interface CommandResult<T = any> {
  success: boolean;
  data?: T;
  error?: string;
}

export interface CommandOptions {
  timeout?: number;
  signal?: AbortSignal;
}

// Plugin types
export interface PluginConfig {
  name: string;
  version: string;
  enabled: boolean;
  config?: Record<string, any>;
}

export interface PluginInfo {
  name: string;
  version: string;
  description: string;
  author: string;
  homepage?: string;
  repository?: string;
  license?: string;
  keywords?: string[];
}

// Database types (if using a database plugin)
export interface DatabaseConnection {
  path: string;
  readonly: boolean;
  inMemory: boolean;
}

export interface DatabaseQuery {
  sql: string;
  params?: any[];
}

export interface DatabaseResult {
  rows: any[];
  rowsAffected: number;
  lastInsertId?: number;
}

// HTTP types (if using HTTP plugin)
export interface HttpRequest {
  url: string;
  method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
  headers?: Record<string, string>;
  body?: string | ArrayBuffer | FormData;
  timeout?: number;
}

export interface HttpResponse {
  status: number;
  statusText: string;
  headers: Record<string, string>;
  data: any;
  url: string;
}

// Clipboard types
export interface ClipboardData {
  text?: string;
  html?: string;
  image?: ArrayBuffer;
}

// Path types
export interface PathInfo {
  exists: boolean;
  isFile: boolean;
  isDir: boolean;
  size?: number;
  modified?: Date;
  created?: Date;
  accessed?: Date;
  permissions?: {
    readonly: boolean;
    writable: boolean;
    executable: boolean;
  };
}

// Utility types for Tauri commands
export type TauriCommand<TArgs = void, TResult = void> = TArgs extends void
  ? () => Promise<TResult>
  : (args: TArgs) => Promise<TResult>;

export type TauriEventListener<T = any> = (
  event: string,
  callback: EventCallback<T>,
  options?: TauriEventOptions
) => Promise<UnlistenFn>;

// Error types
export interface TauriError {
  message: string;
  kind: string;
  info?: any;
}

// Export utility functions types
export interface TauriUtils {
  invoke: <T = any>(
    command: string,
    args?: any,
    options?: TauriInvokeOptions
  ) => Promise<T>;
  listen: TauriEventListener;
  emit: (event: string, payload?: any) => Promise<void>;
  once: TauriEventListener;
}

// Platform-specific types
export type Platform = 'windows' | 'macos' | 'linux';

export interface PlatformInfo {
  platform: Platform;
  arch: string;
  version: string;
  family: string;
}

// Theme types
export interface ThemeColors {
  primary: string;
  secondary: string;
  accent: string;
  background: string;
  surface: string;
  error: string;
  warning: string;
  info: string;
  success: string;
  onPrimary: string;
  onSecondary: string;
  onBackground: string;
  onSurface: string;
  onError: string;
}

export interface ThemeConfig {
  mode: 'light' | 'dark' | 'system';
  colors: ThemeColors;
  borderRadius: number;
  fontSize: {
    xs: string;
    sm: string;
    base: string;
    lg: string;
    xl: string;
    '2xl': string;
    '3xl': string;
    '4xl': string;
  };
  spacing: {
    xs: string;
    sm: string;
    md: string;
    lg: string;
    xl: string;
    '2xl': string;
  };
}
